package com.selftcp;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;

//TCP客户端
public class TCPClient {
    private final DatagramSocket socket;
    private TCPConnection connection;
    
    public TCPClient() throws IOException {
        this.socket = new DatagramSocket();
    }
    
    public TCPConnection connect(InetAddress serverAddr, int serverPort) throws IOException, InterruptedException {
        connection = new TCPConnection(socket.getLocalPort(), serverAddr, serverPort, socket);
        connection.connect();
        return connection;
    }
    
    public void close() {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket.close();
    }
}